local jueyan = fk.CreateSkill{
  name = "lvshi__jueyan",
  tags = { "Phase" }
}

Fk:loadTranslationTable{
  ["lvshi__jueyan"] = "决堰",
  [":lvshi__jueyan"] = "阶段技，你可视为对任意名上下家均与此回合开始时不同的角色使用【水淹七军】，若含有：己方角色，其摸两张牌；你，你于此回合使用牌无次数限制。",
  ["#lvshi__jueyan-choose"] = "你可视为对任意名上下家均与此回合开始时不同的角色使用【水淹七军】",

  ["$lvshi__jueyan1"] = "伤敌当决死，堰毁而泰来！",
  ["$lvshi__jueyan2"] = "堰坝毁，粮道绝，晋军退！",
}

local U = require "packages/utility/utility"
local ls = require "packages/lvshi/utility"
-- ls.isFriend(player, target)
jueyan:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(jueyan.name) and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    table.forEach(room:getAlivePlayers(), function (p)
      return room:setPlayerMark(p, "ls__jueyan-turn", {p:getLastAlive(false).id, p:getNextAlive(false).id})
    end)
  end,
})

jueyan:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  min_target_num = 1,
  max_phase_use_time = 1,
  prompt = "#lvshi__jueyan-choose",
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    if #to_select:getTableMark("ls__jueyan-turn") > 0 then
      return to_select:getTableMark("ls__jueyan-turn")[1] ~= to_select:getLastAlive(false).id and
      to_select:getTableMark("ls__jueyan-turn")[2] ~= to_select:getNextAlive(false).id and not to_select:isRemoved()
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local friends = table.filter(effect.tos, function (p)
      return ls.isFriend(player, p)
    end)
    table.forEach(friends, function (p) return p:drawCards(2, jueyan.name) end)
    if table.contains(effect.tos, player) then
      room:addPlayerMark(player, "lvshi__jueyan-turn")
    end
    room:useVirtualCard("lvshi__drowning", nil, player, effect.tos, jueyan.name, true)
  end,
})

jueyan:addEffect("targetmod", {
  bypass_times = function(self, player, skill, scope, card)
    if player:hasSkill(jueyan.name) and player:getMark("lvshi__jueyan-turn") > 0 then
      return true
    end
  end,
})

return jueyan